/**
 * Created by Administrator on 2018/1/17.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    s=require('./stock');
    api=require('api');
    cn=require('conment');

    module.exports = {
        stockcode:cn.getQueryString('code')||'000001',
        tm :Math.pow(10, 3),
        timestamp: Date.parse(new Date()) / 1000,
        type :"sz",

        data:{},
        intCode:cn.getQueryString('code'),
        history:JSON.parse(cn.getItem('history')),
        init:function () {
            var _this=this;

            function getNowFormatDate() {
                var date = new Date();
                var seperator1 = "-";
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var currentdate = year + seperator1 + month + seperator1 + strDate;
                return currentdate;
            }
            if(_this.intCode==null&&_this.history==null){
                _this.stockcode='000001'
            }else if(_this.intCode){
                _this.stockcode=_this.intCode
            } else if(_this.history.length>0){
                var c=Object.keys(_this.history)
                _this.stockcode=c[0]
            }
            _this.data.stockID=_this.stockcode;
            _this.data.stockType=_this.type;
            _this.data.time=parseInt(_this.timestamp);
            _this.data.hq=eval('"hq_str_'+_this.type+_this.stockcode+'"');
            _this.data.day=""+getNowFormatDate();
            _this.data.isHqReload=eval("true");


            //菜单
            _this.data.menu=[];

            _this.data.menu.push({name:"minute",	title:"分时"});
            _this.data.menu.push({name:"dayK",	title:"日K"});

            //默认加载
            _this.data.default="minute";
            stock.load(_this.data);

            _this.chooseStockInit();
            // _this._temslist();

            $(document).on('click',function (e) {
                var target  = $(e.target);
                if(target.closest(".change-stock").length == 0){
                    $('#search_cue').hide();
                }
            });
            $('#searchConfirm').on('click',function () {
                if($('#search_cue').is(":hidden")){
                    return
                }
                if($('#tbody tr').length>0){
                    var code=$('#tbody tr').eq(0).data('code');
                    window.location.href=cn.GetUrlRelativePath()+'?code='+code;
                }else{
                    cn.alert('请输入查询的股票')
                }
            })
        },
        //搜索
        chooseStockInit:function () {
            var stockChange = $(".search-table"),_this=this;

            $("#searchTxt").off().on("keyup focus", function (event) {

                var key = event.keyCode;
                //13是enter键，38是上键，40是下键
                if (key !== 38 && key !== 40 && key !== 13) {
                    if ($(this).val().length) {
                        var name = $(this).val();
                        _this.seach_stock(name)
                         //把字符串转换为小写
                    } else {
                       _this.seach_history() //调用搜索历史函数
                    }
                } else {
                    event.preventDefault();   //阻止事件
                    var base = $("#search_history, #search_cue").find("tbody:visible");
                    if (base.find("tr.active").length > 0) {
                        if (key === 38) {
                            // goUp(base);
                        } else if (key === 40) {
                            // goDown(base);
                        } else if (key === 13) {
                            // base.find("tr.active").trigger("click");   //触发点击事件
                        }
                    }
                }
                // console.log(6)
            })
        },
        seach_stock:function (n) {
            var _this=this;
            $('#search_cue').show();
            cn.Ajax(api.seach,{
                keyword:n
            },function(d){
                // $('#quickQuery_list').empty();
                var arr=new Array(),list=d.list;
                $.each(list,function(i,l,array){
                   arr.push('<tr data-name="'+l.name+'" data-code="'+l.code+'" data-initial="'+l.initial+'" ><td>'+l.name+'</td><td>'+l.code+'</td><td>'+l.initial+'</td></tr>');
                });
                $('#tbody').html(arr.join(''));
                var host=new Array();
                $('#tbody tr').on('click',function () {
                    var history=JSON.parse(cn.getItem('history'))||{};
                    var name=$(this).data('name');
                    var code=$(this).data('code');
                    var initial=$(this).data('initial');
                    window.location.href=cn.GetUrlRelativePath()+'?code='+code;

                })
            },'post')
        },
        seach_history:function () {
            var _this=this;
            var history=_this.history;
            if(history==null)return
            $('#search_cue').show();
            var arr=new Array(),list=history;
            $.each(list,function(i,l,array){
                arr.push('<tr data-name="'+l.name+'" data-code="'+l.code+'" data-initial="'+l.initial+'" ><td>'+l.name+'</td><td>'+l.code+'</td><td>'+l.initial+'</td></tr>');
            });
            $('#tbody').html(arr.join(''));
            $('#tbody tr').on('click',function () {
                var code=$(this).data('code');
                window.location.href=cn.GetUrlRelativePath()+'?code='+code;
            })
        },


    }

});